﻿using System;
using System.Web.Script.Serialization;
using System.Web.Services;
using HNAlbum.Class;
using HNAlbum.Component.AlbumGallery;
using HNAlbum.Logic.Entity;

namespace HNAlbum
{
    /// <summary>
    /// Home page
    /// </summary>
    public partial class Home : BasePage
    {
        #region Fields
        /// <summary>
        /// Current album dashboard page
        /// </summary>
        private int DashboardPage
        {
            get
            {
                int currentPage = 1;
                if (!string.IsNullOrEmpty(Request.QueryString["page"]))
                {
                    currentPage = int.Parse(Request.QueryString["page"]);
                }
                return currentPage;
            }
        }
        #endregion

        #region Page events
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            //Register JS
            RegisterJS("Home.js");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO: Show album info callout loader
            //Create an instance of album dashboard gallery
            Dashboard dashboard = new Dashboard(DashboardPage, IsGuest);
            pnlDashboard.Controls.Add(dashboard);

            //Then create an instance of the pager based on the dashboard
            DashboardPager pager = new DashboardPager(Dashboard.ITEM_PER_PAGE, dashboard.ItemCount, DashboardPage);
            pnlPager.Controls.Add(pager);

            //Store selected dashboard page to session
            CustomMasterPage.SessionCollection.Page.DashboardPage = DashboardPage;
        }
        #endregion

        #region Web methods
        /// <summary>
        /// Web method to be call by jQuery to get album info
        /// </summary>
        /// <param name="albumId">The album Id</param>
        /// <returns></returns>
        [WebMethod]
        public static string GetAlbumInfo(string albumId)
        {
            //Load the album
            HNAlbumData album = new HNAlbumData(albumId);
            album.Load();
            //Then serialize the album info
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(album.AlbumInfo);
        }
        #endregion
    }
}